<?php
class ZendX_Extjs_View_Helper_Extjs_Container
{
	public $view = null;
	protected $_stylesheets = array();
	protected $_javascriptSources = array();
	protected $_onLoadActions = array();
	
	
	public function setView(Zend_View_Interface $view)
    {
        $this->view = $view;
    }
    
	public function __toString()
    {
        $html  = $this->_renderStylesheets() . PHP_EOL
               . $this->_renderScriptTags() . PHP_EOL
               . $this->_renderExtras();
        return $html;
    }
    
	public function addStylesheet($path)
    {
        $path = (string) $path;
        if (!in_array($path, $this->_stylesheets)) {
            $this->_stylesheets[] = (string) $path;
        }
        return $this;
    }
    
    public function getStylesheets()
    {
        return $this->_stylesheets;
    }
    
	protected function _renderStylesheets()
    {
        foreach ($this->getStylesheets() as $stylesheet) {
            $stylesheets[] = $stylesheet;
        }

        if (empty($stylesheets)) {
            return '';
        }

        array_reverse($stylesheets);
        $style = "";
        foreach($stylesheets AS $stylesheet) {
            if ($this->view instanceof Zend_View_Abstract) {
                $closingBracket = ($this->view->doctype()->isXhtml()) ? ' />' : '>';
            } else {
                $closingBracket = ' />';
            }

            $style .= '<link rel="stylesheet" href="'.$stylesheet.'" '.
                      'type="text/css" media="screen"' . $closingBracket . PHP_EOL;
        }

        return $style;
    }
    
	protected function _renderScriptTags()
    {
        $scriptTags = '';
        
        foreach($this->getJavascriptFiles() AS $javascriptFile) {
        	$scriptTags .= '<script type="text/javascript" src="' . $javascriptFile . '"></script>'.PHP_EOL;
        }        

        return $scriptTags;
    }
 	public function addJavascriptFile($path)
    {
        $path = (string) $path;
        if (!in_array($path, $this->_javascriptSources)) {
            $this->_javascriptSources[] = (string) $path;
        }
        return $this;
    }
    
    public function getJavascriptFiles()
    {
        return $this->_javascriptSources;
    }
    
    public function clearJavascriptFiles()
    {
        $this->_javascriptSources = array();
        return $this;
    }
 	protected function _renderExtras()
    {
        $onLoadActions = array();        
        foreach ($this->getOnLoadActions() as $callback) {
        	$onLoadActions[] = $callback;
       	}             

        $content = "
      	Ext.Loader.setConfig({enabled: true});
		Ext.Loader.setPath('Ext.ux', '/Extjs/ux');
		Ext.require([					
		             'Ext.grid.*',					
		             'Ext.data.*',	
		             'Ext.dd.*',				
		             'Ext.ux.grid.FiltersFeature',					
		             'Ext.toolbar.Paging'					
		         ]);
        ";

        if (!empty($onLoadActions)) {            
            $content .= 'Ext.onReady(function(){'."\n    ";
            
            $content .= implode("\n    ", $onLoadActions) . "\n";
            $content .= '});'."\n";
        }        

        if (preg_match('/^\s*$/s', $content)) {
            return '';
        }

        $html = '<script type="text/javascript">' . PHP_EOL
              . (($this->view->doctype()->isXhtml()) ? '//<![CDATA[' : '//<!--') . PHP_EOL
              . $content
              . (($this->view->doctype()->isXhtml()) ? '//]]>' : '//-->') . PHP_EOL
              . PHP_EOL . '</script>';
        return $html;
    }
    
	public function addOnLoad($callback)
    {
        if (!in_array($callback, $this->_onLoadActions, true)) {
            $this->_onLoadActions[] = $callback;
        }
        
        return $this;
    }

    
    public function getOnLoadActions()
    {
        return $this->_onLoadActions;
    }

    
    public function clearOnLoadActions()
    {
        $this->_onLoadActions = array();
        return $this;
    }
}